package com.fengxu.gamescript.ui.main.mode.msg

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.*
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import com.alibaba.fastjson.JSONObject
import com.fengxu.gamescript.R
import com.fengxu.gamescript.data.Conf
import com.fengxu.gamescript.data.GlobalData
import com.fengxu.gamescript.data.po.Music
import com.fengxu.gamescript.provide.ProviderFactory
import com.fengxu.gamescript.ui.main.MainActivity
import com.fengxu.gamescript.util.CommonUtils
import com.fengxu.gamescript.util.PermissionUtils
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.squareup.picasso.Picasso
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog
import com.xuexiang.xui.widget.toast.XToast
import org.jetbrains.anko.support.v4.runOnUiThread
import org.jetbrains.anko.support.v4.toast
import java.io.File
import kotlin.properties.Delegates

/**
 * 消息页
 */
@SuppressLint("JavascriptInterface")
class MsgFragment : Fragment() {

    // 是否初始化了该页
    private var isInit = false

    // 用户对该页是否可见
    private var userCanLookThis = false

    // 提示加载的弹窗
    private var loadingDialog: MaterialDialog by Delegates.notNull()

    private val webView:WebView by lazy {
        requireActivity().findViewById(R.id.msg_webview)
    }

    private val smartRefreshLayout:SmartRefreshLayout by lazy {
        requireActivity().findViewById(R.id.msg_smartRefresh)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_msg, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 初始化webView
        webView.settings.javaScriptEnabled = true
        webView.settings.domStorageEnabled = true
        webView.addJavascriptInterface(this, "android")
        (requireActivity() as MainActivity).run {
            tabSelectChangeEvents.add(this@MsgFragment::tabChanged)
        }
        // 刷新事件
        smartRefreshLayout.setOnRefreshListener {
            loadWebView()
            it.finishRefresh()
        }
    }


    /**
     * tab选项改变事件
     */
    private fun tabChanged(index: Int){
        userCanLookThis = false
        if(index == 2){
            userCanLookThis = true
            if(!isInit){
                loadWebView(true)
                isInit = true
            }
        }
    }

    /**
     * webView加载网页
     * @param 是否显示加载弹窗
     */
    private fun loadWebView(showLoading: Boolean = false){
        if(showLoading){
            loadingDialog = CommonUtils.loadingDialog(requireContext())
        }
        webView.loadUrl("file:///android_asset/doc/index.html")
    }

    /**
     * 加载消息文档
     */
    @JavascriptInterface
    fun loadDoc():String{
        val bytes = requireActivity().assets.open("doc/msg.md").readBytes()
        return String(bytes)
    }

    /**
     * 加载完成
     */
    @JavascriptInterface
    fun loadComplete(){
        loadingDialog?.dismiss()
    }

}